import Vue from 'vue';
import VueRouter from 'vue-router';
import Login from '../components/login.vue'; //导入登陆组件
import Home from '../components/home.vue' //导入后台管理页面组件
import Welcome from '../components/welcome.vue' //导入welcome组件
import UserList from '../components/user/userList.vue' //导入用户列表组件
import RoleList from '../components/power/roleList.vue' //导入角色列表组件
import PowerList from '../components/power/powerList.vue' //导入权限列表组件
import ShopList from '../components/goods/shopList.vue' //导入商品列表组件
import addGoods from '../components/goods/addGoods.vue' //导入商品添加组件
import editGoods from '../components/goods/editGoods.vue' //导入商品修改组件
import shopSort from '../components/goods/shopSort.vue' //导入商品分类组件
import SortList from '../components/goods/sortList.vue' //导入分类参数组件
import BuyList from '../components/orders/buyList.vue' //导入订单列表组件
import DataList from '../components/report/dataList.vue' //导入数据报表列表组件


Vue.use(VueRouter)

const router = new VueRouter({
  routes: [{
      path: '/',
      redirect: '/login'
    },
    {
      path: '/login',
      component: Login
    },
    {
      path: '/home',
      redirect: '/welcome',
      component: Home,
      children: [{
          path: '/welcome',
          component: Welcome
        }, {
          path: '/users',
          component: UserList
        }, {
          path: '/roles',
          component: RoleList
        }, {
          path: '/rights',
          component: PowerList
        }, {
          path: '/goods',
          component: ShopList,
        }, {
          path: '/params',
          component: SortList
        }, {
          path: '/categories',
          component: shopSort
        }, {
          path: '/orders',
          component: BuyList
        }, {
          path: '/reports',
          component: DataList
        }, {
          path: '/goods/add',
          component: addGoods
        },
        {
          path: '/goods/edit',
          component: editGoods
        }
      ]
    }

  ]
})

//挂载路由导航守卫
router.beforeEach((to, from, next) => { //beforeEach为路由导航守卫
  //to将要访问的路径
  //from代表从哪个路径跳转而来
  //next是一个函数，表示放行  next() 放行  next('/login)强制跳转
  //如果用户直接访问login页面直接放行
  if (to.path == '/login') return next();
  //从sessionStorage中获取保存的token值，判断是否有token
  const token = window.sessionStorage.getItem('token');
  //如没有token就强制跳转到login页面
  if (!token) return next('/login');
  //如果有token就放行
  next();
})

export default router;
